package org.feifei.product.service.impl;

import org.feifei.basic.service.impl.BaseServiceImpl;
import org.feifei.basic.util.LoginContextUtil;
import org.feifei.org.mapper.ShopMapper;
import org.feifei.product.domain.Product;
import org.feifei.product.domain.ProductDetail;
import org.feifei.product.mapper.ProductDetailMapper;
import org.feifei.product.mapper.ProductMapper;
import org.feifei.product.service.ProductService;
import org.feifei.user.domain.LoginInfo;
import org.feifei.user.domain.User;
import org.feifei.user.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements ProductService {

    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductDetailMapper productDetailMapper;
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public void editORSave(Product product) {
        ProductDetail detail = product.getDetail();
//        product.setType_id(product.getType().getId());
        if (product.getId() == null) {
            //添加宠物信息
            super.save(product);
            //添加明细
            if(detail != null){
                detail.setProduct_id(product.getId());
                productDetailMapper.save(detail);
            }
        } else {
            if (detail != null) {
                productDetailMapper.update(detail);
            }
            productMapper.update(product);
        }

    }

    @Override
    public void onsale(List<Long> ids) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("onsaleTime",new Date());
        map.put("ids", ids);
        productMapper.onsale(map);
    }

    @Override
    public void offsale(List<Long> ids) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("offsaleTime",new Date());
        map.put("ids", ids);
        productMapper.offSale(map);
    }

    @Override
    public void adopt(Long id, HttpServletRequest request) {
        Product product = productMapper.get(id);
        LoginInfo loginInfo = LoginContextUtil.getLoginInfo(request);
        User user = userMapper.getByLoginId(loginInfo.getId());
//        product.setUser_id(user.getId()).setOffsaletime(new Date()).setState(0);
        productMapper.update(product);
    }

    @Override
    public void remove(Long id) {
        productDetailMapper.removeByProductId(id);
        super.remove(id);
    }

    @Override
    public void batchRemove(List<Long> ids) {
        productDetailMapper.batchRemoveByProductIds(ids);
        super.batchRemove(ids);
    }

    @Override
    public void save(Product product) {
        ProductDetail detail = product.getDetail();
        //添加宠物信息
        super.save(product);
        //添加明细
        if(detail != null){
            detail.setProduct_id(product.getId());
            productDetailMapper.save(detail);
        }
    }

    @Override
    public Product get(Long id) {

        Product product = super.get(id);
        ProductDetail productDetail = productDetailMapper.getByProductId(id);
//        if (product.getShop_id() != null) {
//            product.setShop(shopMapper.get(product.getShop_id()));
//        }
        product.setDetail(productDetail);
        return product;
    }
}
